/*
 * Md5.h
 *
 *  Created on: 04/12/2010
 *      Author: santiago
 */

#ifndef MD5_H_
#define MD5_H_

#include <stdio.h>
#include <string>
#include <iostream>
#include <memory.h>

using namespace std;

class MD5 {

public:
  typedef unsigned int size_type;

  MD5();
  MD5(const string& text);
  void update(const unsigned char *buf, size_type length);
  void update(const char *buf, size_type length);
  MD5& finalize();
  string hexdigest() const;
  friend ostream& operator<<(ostream&, MD5 md5);

private:
  void init();
  typedef unsigned char uint1;
  typedef unsigned int uint4;
  enum {blocksize = 64};

  void transform(const uint1 block[blocksize]);
  static void decode(uint4 output[], const uint1 input[], size_type len);
  static void encode(uint1 output[], const uint4 input[], size_type len);

  bool finalized;
  uint1 buffer[blocksize];
  uint4 count[2];
  uint4 state[4];
  uint1 digest[16];

  static inline uint4 F(uint4 x, uint4 y, uint4 z);
  static inline uint4 G(uint4 x, uint4 y, uint4 z);
  static inline uint4 H(uint4 x, uint4 y, uint4 z);
  static inline uint4 I(uint4 x, uint4 y, uint4 z);
  static inline uint4 rotate_left(uint4 x, int n);
  static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
};

string md5(const string str);

#endif /* MD5_H_ */
